package com.adtech.core;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.Resource;

/**
 * 全局配置类
 * 
 * @author Dormin
 * @since 2012-3-20
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class Settings {
	
	private static Properties settings = new Properties();
	
	public static final String TMPDIR_PATH = "tmpdir.path";
	private static final String CACHE_KEY = "cache.enable";
	private static final String FILE_BUFFER_SIZE_KEY = "file.buffer.size";
	private static final String FILE_WAIT_INTERVAL_KEY = "file.wait.interval";
	private static final String SYNC_NODE_KEY = "sync.node.key";
	public static long FILE_BUFFER_SIZE = 0;
	public static long FILE_WAIT_INTERVAL = 0;
	public static Boolean CACHE = false;
	public static String SYNC_NODE = null;
	
	static {
		settings.setProperty(TMPDIR_PATH, System.getProperty("java.io.tmpdir"));
	}
	
	public static void init() {
		Settings.CACHE = Settings.getBoolean(CACHE_KEY);
		FILE_BUFFER_SIZE = Settings.getLong(FILE_BUFFER_SIZE_KEY)*1024L*1024L;
		FILE_WAIT_INTERVAL = Settings.getLong(FILE_WAIT_INTERVAL_KEY);
		SYNC_NODE = Settings.get(SYNC_NODE_KEY);
	}

	/**
	 * 加载properties文件中的内容
	 *
	 * @param properties *.properties文件资源
	 * @throws IOException 文件不存在或者读取失败时，抛出此异常
	 *
	 * @author Dormin
	 */
	public synchronized static void load(Resource properties) throws IOException{
		settings.load(properties.getInputStream());
	}
	
	public static void set(String property, String value){
		settings.setProperty(property, value);
	}
	
	public static String get(String property){
		return settings.getProperty(property);
	}

	public static int getInt(String property) {
		return Integer.valueOf(get(property));
	}
	
	public static long getLong(String property) {
		return Long.valueOf(get(property));
	}
	
	public static double getDouble(String property) {
		return Double.valueOf(get(property));
	}
	
	public static float getFloat(String property) {
		return Float.valueOf(get(property));
	}
	
	public static boolean getBoolean(String property) {
		return Boolean.valueOf(get(property));
	}
}
